/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-11-26
 * Time: 23:20
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Labb4.Media.Graphics
{
	/// <summary>
	/// A wrapper class for an object of type Image, holds some information about the Image,
	/// like transparent color
	/// </summary>
	public class Picture
	{
		#region members
		
		/// <summary>
		/// The Image
		/// </summary>
		private Image				m_ImageData;
		/// <summary>
		/// Attributes of the image, here is where the transparent color is stored for use
		/// in the system
		/// </summary>
		private ImageAttributes		m_Attributes;
		/// <summary>
		/// If the transparent color is set, this will be true, otherwise false
		/// </summary>
		private bool				m_KeyIsSet;
		/// <summary>
		/// The transparanet color, for outside use
		/// </summary>
		private Color				m_ColorKey;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, sets members, attempts to load the Image from the given file name. If
		/// the file can not be found, the application will exit after having shown an error message.
		/// </summary>
		/// <param name="fileName">The file name where the image data is stored</param>
		public Picture(string fileName)
		{
			try
			{
				m_ImageData = Image.FromFile(fileName);
				MediaManager.Instance.AddPicture(fileName, this);
			}
			catch
			{
				MessageBox.Show("The file " + fileName + " was not found. This file is needed for this program to start and run properly. Please reinstall the application. This program will now terminate!", "Missing file");
				Application.Exit();
			}
			
			m_Attributes = null;
			m_KeyIsSet = false;
		}
		
		/**
		 * Is it possible to comment the destructor?
		 * Anyways, calls release, in case this has been forgotten.
		 */
		~Picture()
		{
			Release();
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Sets the color key, the color which should be concidered transparent.
		/// </summary>
		/// <param name="red">The red component of the transparent color</param>
		/// <param name="green">The green component of the transparent color</param>
		/// <param name="blue">The blue component of the transparent color</param>
		public void SetTransparentColor(int red, int green, int blue)
		{
			m_ColorKey = Color.FromArgb(red, green, blue);
			m_Attributes = new ImageAttributes();
			m_Attributes.SetColorKey(m_ColorKey, m_ColorKey);
			
			m_KeyIsSet = true;
		}
		
		/// <summary>
		/// Checks to see if a given point is a color or not. The point is concidered
		/// to be "not a color" if it is of the same color as the color key. If the
		/// color key (the transparent color) has not been set, all points within the
		/// range of the picture ((0, 0) to (width, height)) will be concidered
		/// color. All points outside the range of the picture is concidered to be
		/// "not a color"
		/// </summary>
		/// <param name="x">The x coordinate to check, relative to the image</param>
		/// <param name="y">The y coordinate to check, relative to the image</param>
		/// <returns></returns>
		public bool PointIsColor(ref int x, ref int y)
		{
			bool isColor = true;
			
			if((x < 0) || (x > m_ImageData.Width) || (y < 0) || (y > m_ImageData.Height))
			{
				isColor = false;
			}
			else
			{
				if(m_KeyIsSet)
				{
					Color color = ((Bitmap)m_ImageData).GetPixel(x, y);
					
					if(color.Equals(m_ColorKey))
					{
						isColor = false;
					}
				}
			}
			
			return isColor;
		}
		
		/// <summary>
		/// Releases the image data from memory
		/// </summary>
		public void Release()
		{
			if(m_ImageData != null)
			{
				m_ImageData.Dispose();
				m_ImageData = null;
			}
		}
		
		#endregion
		
		#region properties
		
		/// <summary>
		/// Reference to the image data (get)
		/// </summary>
		public Image ImageData
		{
			get
			{
				return m_ImageData;
			}
		}
		
		/// <summary>
		/// True if the color jey has been set (get)
		/// </summary>
		public bool KeyIsSet
		{
			get
			{
				return m_KeyIsSet;
			}
		}
		
		/// <summary>
		/// The image attributes, containing the color key readable to the system (get)
		/// </summary>
		public ImageAttributes Attributes
		{
			get
			{
				return m_Attributes;
			}
		}
		
		#endregion
	}
}
